package cn.pink.gamesrv.account;

import cn.pink.common.event.EventKey;
import cn.pink.common.proto.Msg;
import cn.pink.common.support.tools.global.GlobalTool;
import cn.pink.core.CallPoint;
import cn.pink.core.Chunk;
import cn.pink.core.Port;
import cn.pink.core.Session;
import cn.pink.core.support.Param;
import cn.pink.core.support.observer.EventObserver;
import cn.pink.gamesrv.RPCProxy;

/**
 * 账号管理器
 * @Author: pink
 * @Date: 2022/6/30 17:58
 */
public class AccountManager {
    @EventObserver(EventKey.ACCOUNT_LOGIN)
    public static void login(Param param) {

    }

    /**
     * 角色登陆
     */
    public static void roleLogin(AccountObject accObj, String humanId, AccountService serv) {
        Session session = GlobalTool.getInstance().getSession(humanId);

        CallPoint gamePoint = new CallPoint();

        //登录前check 其他game是否存在角色 存在直接发到原来玩家在线的game登录
        if(session != null) {
            gamePoint.nodeId = session.getGamePoint().nodeId;
            gamePoint.portId = session.getGamePoint().portId;
        }
        else {
            Port port = Port.getCurrent();

            gamePoint.nodeId = port.getNodeId();
            gamePoint.portId = port.getId();
        }

        // 开始角色登陆
        RPCProxy.GameServiceProxy proxy = RPCProxy.GameServiceProxy.newInstance(gamePoint.nodeId, gamePoint.portId);
        proxy.roleLogin(accObj.getSessionPoint(), humanId, accObj.getUserIdentity(), accObj.getToken());

        proxy.listenResult((Param result, Param context) -> {
            //TODO 带角色登录 删除 AccountObj

            // 成功协议
            Msg.SCRoleLogin.Builder msg = Msg.SCRoleLogin.newBuilder();
            msg.setResultCode(0);

            // 发送并绑定
            cn.pink.gatesrv.RPCProxy.SessionServiceProxy proxy1 = cn.pink.gatesrv.RPCProxy.SessionServiceProxy.newInstance(accObj.getSessionPoint());
            proxy1.loginResult(humanId, gamePoint, new Chunk(msg));
        });
    }
}
